package com.lzh.financial.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzh.financial.bean.PUserBean;
import com.lzh.financial.bean.PUserInfoBean;
import com.lzh.financial.dao.IUserInfoMapper;
import com.lzh.financial.dao.IUserMapper;
import com.lzh.financial.dao.IUserMoneyMapper;
import com.lzh.financial.service.IUserInfoService;
import com.lzh.financial.util.JwtTokenUtil;
import com.lzh.financial.util.MapperFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author reborn0530
 * 用户service实现类
 */
@Service
@Slf4j
public class UserInfoServiceImpl extends ServiceImpl<IUserInfoMapper, PUserInfoBean> implements IUserInfoService {


    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    @Qualifier("userDetailsServiceImpl")
    private UserDetailsService userDetailsService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;


    @Override
    public String login(String username, String password) {
        UsernamePasswordAuthenticationToken upToken = new UsernamePasswordAuthenticationToken(username, password);
        Authentication authentication = authenticationManager.authenticate(upToken);
        SecurityContextHolder.getContext().setAuthentication(authentication);
        UserDetails userDetails = userDetailsService.loadUserByUsername(username);
        System.out.println(userDetails);
        return jwtTokenUtil.generateToken(userDetails);
    }

    @Override
    public PUserInfoBean getGroupWorkByUserId(Integer userId) {
        QueryWrapper<PUserInfoBean> wrapper = new QueryWrapper<>();
        String[] a={"user_id","work","group_id"};
        PUserInfoBean pUserInfoBean = getBaseMapper().selectOne(wrapper.select(a).eq("user_id",userId));
        return pUserInfoBean;
    }


}
